﻿using Orchard;
using Orchard.Mvc.Filters;
using System.Web.Mvc;
using Wolf.LoggedInUsers.Services;

namespace Wolf.LoggedInUsers.Filters
{
    public class UserActionFilter : FilterProvider, IActionFilter
    {
        private readonly ILoggedInUsersService _loggedInUsersService;
        private readonly IWorkContextAccessor _wca;

        public UserActionFilter(ILoggedInUsersService loggedInUsersService, IWorkContextAccessor wca)
        {
            _loggedInUsersService = loggedInUsersService;
            _wca = wca;
        }

        public void OnActionExecuted(ActionExecutedContext filterContext) { }

        public void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var user = _wca.GetContext().CurrentUser;
            if (user != null) _loggedInUsersService.UpdateUser(user);
        }
    }
}